using System;
using System.IO;
using QFramework.Framework.Util;
using UnityEngine;

namespace QFramework.Framework.ResKit.HotUpdate
{
    /// <summary>
    ///     热更新状态
    /// </summary>
    public enum HotUpdateState
    {
        /// <summary>
        ///     从未更新过
        /// </summary>
        NeverUpdate,

        /// <summary>
        ///     更新过
        /// </summary>
        Updated,

        /// <summary>
        ///     覆盖安装过，但是有旧的资源
        /// </summary>
        Overrided
    }

    /// <summary>
    ///     热更新管理类
    /// </summary>
    public class HotUpdateMgr : MonoSingleton<HotUpdateMgr>
    {
        /// <summary>
        ///     热更新状态
        /// </summary>
        public HotUpdateState State { get; private set; }

        /// <summary>
        ///     热更新配置
        /// </summary>
        public HotUpdateConfig Config { get; set; }

        private void Awake()
        {
            Config = new HotUpdateConfig();
        }

        public void CheckState(Action done)
        {
            var persistentResVersion = Config.LoadHotUpdateAssetBundlesFolderResVersion();

            if (persistentResVersion == null)
            {
                State = HotUpdateState.NeverUpdate;
                done();
            }
            else
            {
                StartCoroutine(Config.GetStreamingAssetResVersion(streamingResVersion =>
                {
                    if (persistentResVersion.version > streamingResVersion.version)
                        State = HotUpdateState.Updated;
                    else
                        State = HotUpdateState.Overrided;

                    done();
                }));
            }
        }

        /// <summary>
        ///     获取本地资源版本
        /// </summary>
        /// <returns></returns>
        public void GetLocalResVersion(Action<int> getLocalResVersion)
        {
            if (State == HotUpdateState.NeverUpdate || State == HotUpdateState.Overrided)
            {
                StartCoroutine(Config.GetStreamingAssetResVersion(resVersion =>
                {
                    getLocalResVersion(resVersion.version);
                }));
                return;
            }

            var localResVersion = Config.LoadHotUpdateAssetBundlesFolderResVersion();
            getLocalResVersion(localResVersion.version);
        }

        /// <summary>
        ///     是否有新版本资源
        /// </summary>
        /// <param name="onResult"></param>
        public void HasNewVersionRes(Action<bool> onResult)
        {
            FakeResServer.Instance.GetRemoteResVersion(remoteResVersion =>
            {
                GetLocalResVersion(localResVersion =>
                {
                    var result = remoteResVersion > localResVersion;
                    onResult(result);
                });
            });
        }

        /// <summary>
        ///     更新资源
        /// </summary>
        /// <param name="onUpdateDone"></param>
        public void UpdateRes(Action onUpdateDone)
        {
            Debug.Log("开始更新");
            Debug.Log("1.下载资源");

            FakeResServer.Instance.DownloadRes(() =>
            {
                ReplaceLocalRes();

                Debug.Log("结束更新");

                onUpdateDone();
            });
        }

        /// <summary>
        ///     替换资源
        /// </summary>
        private void ReplaceLocalRes()
        {
            Debug.Log("2.替换掉本地资源");

            var tempAssetBundleFolders = FakeResServer.TempAssetBundlesPath;
            var assetBundlesFolders = Config.HotUpdateAssetBundlesFolder;

            if (Directory.Exists(assetBundlesFolders))
                Directory.Delete(assetBundlesFolders, true);

            Directory.Move(tempAssetBundleFolders, assetBundlesFolders);

            if (Directory.Exists(tempAssetBundleFolders))
                Directory.Delete(tempAssetBundleFolders, true);
        }
    }
}